package com.suray.wcs.web.system.controller;

import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.controller.ResponseData;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.core.utils.SurayBeanUtils;
import com.suray.commin.enmus.BusinessType;
import com.suray.wcs.service.system.bo.PageResultBO;
import com.suray.wcs.service.system.bo.SingleRuleBO;
import com.suray.wcs.service.system.entity.SingleRuleDB;
import com.suray.wcs.service.system.service.ISingleRuleService;
import com.suray.wcs.web.system.request.AddSingleRuleRequest;
import com.suray.wcs.web.system.request.ApplySingleRuleRequest;
import com.suray.wcs.web.system.request.UpdateSingleRuleRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/singleRule")
@CrossOrigin
public class SingleRuleController extends BaseController {
    @Autowired
    private ISingleRuleService singleRuleService;

    @PreAuthorize("@ss.hasPermi('singleRule:list')")
    @GetMapping(value = "/getSingleRuleList")
    @ResponseBody
    @ApiOperation(value = "获取深位配置列表")
    public TableDataInfo getSingleRuleList(SingleRuleDB singleRuleDB) {
        startPage();
        List<SingleRuleDB> singleRuleList = singleRuleService.getSingleRuleList(singleRuleDB);
        return getDataTable(singleRuleList);
    }

    @GetMapping(value = "/getSingleRuleInfo/{ruleId}")
    @ResponseBody
    @ApiOperation(value = "getSingleRuleInfo")
    public ResponseData getSingleRuleInfo(@PathVariable Integer ruleId) {
        PageResultBO singleRuleInfo = singleRuleService.getSingleRuleInfo(ruleId);
        return ResponseData.success("深位规则查询完成", singleRuleInfo);
    }

    @GetMapping(value = "/getSingleRuleDirectionList")
    @ResponseBody
    @ApiOperation(value = "获取深位配置方向列表")
    public ResponseData getSingleRuleDirectionList() {
        return ResponseData.success(singleRuleService.getSingleRuleDirectionList());
    }

    @PreAuthorize("@ss.hasPermi('singleRule:add')")
    @Log(title = "深位配置管理", method = "添加深位配置", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addSingleRule")
    @ResponseBody
    @ApiOperation(value = "添加深位配置")
    public ResponseData addSingleRule(@RequestBody AddSingleRuleRequest request) {
        if (request == null || request.checkParams()) {
            return ResponseData.error("请求参数有误");
        }
        SingleRuleBO singleRuleBO = SurayBeanUtils.copyProperties(request, SingleRuleBO.class);
        singleRuleService.addSingleRule(singleRuleBO);
        return ResponseData.success("添加深位配置成功");
    }

    @PreAuthorize("@ss.hasPermi('singleRule:edit')")
    @Log(title = "深位配置管理", method = "更新深位配置", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateSingleRule")
    @ResponseBody
    @ApiOperation(value = "更新深位配置")
    public ResponseData updateSingleRule(@RequestBody UpdateSingleRuleRequest request) {
        if (request == null || request.checkParams()) {
            return ResponseData.error("请求参数有误");
        }
        SingleRuleBO singleRuleBO = SurayBeanUtils.copyProperties(request, SingleRuleBO.class);
        singleRuleService.updateSingleRule(singleRuleBO);
        return ResponseData.success("更新深位配置成功");
    }

    @PreAuthorize("@ss.hasPermi('singleRule:remove')")
    @Log(title = "深位配置管理", method = "删除深位配置", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/deleteSingleRule/{ruleIds}")
    @ResponseBody
    @ApiOperation(value = "删除深位配置")
    public ResponseData deleteSingleRule(@PathVariable List<Integer> ruleIds) {
        if (ruleIds == null || ruleIds.size() < 1) {
            return ResponseData.error("请求参数有误");
        }
        singleRuleService.deleteSingleRule(ruleIds);
        return ResponseData.success("删除深位配置成功");
    }

    @Log(title = "深位配置管理", method = "应用深位配置", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/applySingleRule")
    @ResponseBody
    @ApiOperation(value = "应用深位配置")
    public ResponseData applySingleRule(@RequestBody ApplySingleRuleRequest request) {
        if (request == null || request.getIds() == null || request.getIds().size() < 1) {
            return ResponseData.error("请求参数有误");
        }
        singleRuleService.applySingleRule(request.getIds());
        return ResponseData.success("应用深位配置成功");
    }
}
